const DB = wx.cloud.database().collection('focus_list');
const util = require("../../utils/util.js");
const app = getApp();
let varName;
let animation;
let timeInterval;
let timeSub;
let ctx = wx.createCanvasContext('canvasArcCir');
Page({

  data: {
    title: "专注",
    isTransform: true,
    isBack: false,
    countMinute: null,
    begintime:null,
    counttime:null,
    endtime:null,
    countSecond: null,
    time: null,
    stop: true,
  },

  drawCircle: function (time) {
    clearInterval(varName);

    function drawArc(s, e) {
      ctx.setFillStyle('white');
      ctx.clearRect(0, 0, 200, 200);
      ctx.draw();
      var x = 150,
        y = 150,
        radius = 120;
      ctx.setLineWidth(4);
      ctx.setStrokeStyle('#000000');
      ctx.setLineCap('round');
      ctx.beginPath();
      ctx.arc(x, y, radius, s, e, false);
      ctx.stroke()
      ctx.draw()
    }
    var step = 1,
      startAngle = 1.5 * Math.PI,
      endAngle = 0;
    // var animation_interval = 1000;
    animation = function () {
      if (step <= time) {
        endAngle = step * 2 * Math.PI / time + 1.5 * Math.PI;
        drawArc(startAngle, endAngle);
        step++;
      } else {
        clearInterval(varName);
      }
    };
    varName = setInterval(animation, 1000);
  },
  countDown(time) {
    let that = this
    // 设置开始时间，1天=86400秒
    var totalSecond = time - 1;
    timeSub = function () {
      //Math.floor()向下取整

      // 分
      var minute = Math.floor((totalSecond) / 60);
      if (minute < 10) minute = '0' + minute;

      // 秒
      var second = totalSecond - minute * 60;
      if (second < 10) second = '0' + second;

      this.setData({
        countMinute: minute,
        countSecond: second,
      });
      totalSecond--;
      if (totalSecond < 0) {
        let endtime = util.formatDate(new Date())[1]
        clearInterval(timeInterval);
        wx.showToast({
          title: '专注结束',
        })
        that.setData({
          countMinute: '00',
          endtime:endtime,
          countSecond: '00',
        });
        wx.navigateBack({
          delta: 1,
        })
        that.addFocus(true)
      }
    }.bind(that)
    // 设置定时器
    timeInterval = setInterval(timeSub, 1000);
  },
  isStop() {
    let that = this;
    let stop = that.data.stop;
    if (stop) {
      clearInterval(varName);
      clearInterval(timeInterval);
      that.setData({
        stop: false
      })
    } else {
      varName = setInterval(animation, 1000);
      timeInterval = setInterval(timeSub, 1000);
      that.setData({
        stop: true
      })
    }
  },
  exit() {
    let that = this
    clearInterval(varName);
    clearInterval(timeInterval);
    let endtime = util.formatDate(new Date())[1]
    that.setData({
      endtime:endtime
    })
    wx.navigateBack({
      delta: 1,
    })
    that.addFocus(false)
  },
  addFocus(isDid){
    let that = this
    let user = {
      state: isDid
    };
    let date = {
      begintime:that.data.begintime,
      endtime:that.data.endtime,
      countMinute:that.data.counttime,
      date:util.formatDate(new Date())[0]
    }
    let all = util.extend(user,date)
    console.log(all);
    DB.add({
      data: all,
      success: res => {
        console.log("添加成功", res);
      },
      fail: res => {
        console.log("添加失败", res);
      }
    })
  },
  onLoad: function (options) {
    let that = this;
    let begintime = util.formatDate(new Date())[1]
    let time = options.time * 60;
    that.setData({
      counttime:options.time,
      countMinute: options.time,
      begintime: begintime,
      countSecond: '00',
      time: time
    })
  },
  onReady: function () {
    let that = this;
    let time = that.data.time;
    that.drawCircle(time);
    that.countDown(time);
    var cxt_arc = wx.createCanvasContext('canvasCircle');
    cxt_arc.setLineWidth(3);
    cxt_arc.setStrokeStyle('#eaeaea');
    cxt_arc.setLineCap('round');
    cxt_arc.beginPath();
    cxt_arc.arc(150, 150, 120, 0, 2 * Math.PI, false);
    cxt_arc.stroke();
    cxt_arc.draw();
  }
})